#include <stdio.h>
#include <string.h>

int search(char sets[50][50], int m, int n, int x_pos, int y_pos, int direction){
	// temp variables
	int i,j,dec=0;
	
	// direction =0  -> initial condition
	if(direction==0){
		for(i=1;i<9;i++)	if(search(sets,m,n,x_pos,y_pos,i))	dec=1;
		return dec;
	} else{
	}
}


int main(){
	
	// define the variables
	char sets[50][50], keywords[20][51];
	int m,n,cases,k;
	// the temp variables
	int i,j;
	char str[100];	// temp string to hold the input

	// input:
	//
	// cases: * the num of cases following
	scanf("%d",&cases);

	// [blank line]
	// * [in each case: ]
	while(cases--){
	//  m,n: m - # of lines; n - # of letters
		scanf("%d%d",&m,&n);
	//   * m*n grid
		//fflush(stdin);
		for(i=0;i<m;i++){
			getchar();	// the former '\n' char undone.
			for(j=0;j<n;j++)	sets[i][j]=tolower(getchar());
			sets[i][j]=0;	// for test use only
			//getchar();
		}
	//  k : # of list of words to search for
		scanf("%d",&k);
	//   * k num. of words
		for(i=0;i<k;i++)	scanf("%s",keywords[i]);
	// output:
	//	(all words can be found at least once)
	// the pos of the first appearance: [top-left first]
	// [row_pos, col_pos]	-- start from 1
	// [blank line], then next
	   for(i=0;i<m;i++)    printf("%s\n",sets[i]);
	   putchar('\n');
	}
	getch();
	return 0;
}
